﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Possan.Localization;
using System.Globalization;

namespace Loot
{
	public partial class Phrase : Form
	{
		public Phrase()
		{
			InitializeComponent();
			Provider = null;
			selectedLocales = new List<string>();
		}

		public ILibraryProvider Provider { get; set; }
		public List<string> selectedLocales;
		public string selectedNamespace;
		public string selectedId;

		private void Phrase_Load(object sender, EventArgs e)
		{
			laCode1.Text = "?";
			laName1.Text = "?";
			tbTranslation1.Text = "";
			tbTranslation1.Enabled = false;

			laCode2.Text = "?";
			laName2.Text = "?";
			tbTranslation2.Text = "";
			tbTranslation2.Enabled = false;

			laCode3.Text = "?";
			laName3.Text = "?";
			tbTranslation3.Text = "";
			tbTranslation3.Enabled = false;

			if (selectedLocales.Count > 0 && selectedLocales[0] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[0]);
				laCode1.Text = ci.Name;
				laName1.Text = ci.EnglishName;
				tbTranslation1.Text = Provider.TranslatePhraseByID(selectedNamespace, selectedId, "", ci);
				tbTranslation1.Enabled = true;
			}

			if (selectedLocales.Count > 1 && selectedLocales[1] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[1]);
				laCode2.Text = ci.Name;
				laName2.Text = ci.EnglishName;
				tbTranslation2.Text = Provider.TranslatePhraseByID(selectedNamespace, selectedId, "", ci);
				tbTranslation2.Enabled = true;
			}

			if (selectedLocales.Count > 2 && selectedLocales[2] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[2]);
				laCode3.Text = ci.Name;
				laName3.Text = ci.EnglishName;
				tbTranslation3.Text = Provider.TranslatePhraseByID(selectedNamespace, selectedId, "", ci);
				tbTranslation3.Enabled = true;
			}

		}

		void saveClose( bool close )
		{
			if (selectedLocales.Count > 0 && selectedLocales[0] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[0]);
				Provider.RegisterPhraseID(selectedNamespace, selectedId, "", tbTranslation1.Text, ci);
			}

			if (selectedLocales.Count > 1 && selectedLocales[1] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[1]);
				Provider.RegisterPhraseID(selectedNamespace, selectedId, "", tbTranslation2.Text, ci);
			}

			if (selectedLocales.Count > 2 && selectedLocales[2] != "")
			{
				CultureInfo ci = new CultureInfo(selectedLocales[2]);
				Provider.RegisterPhraseID(selectedNamespace, selectedId, "", tbTranslation3.Text, ci);
			}

			if( close )
				Close();
		}

		private void tbTranslation1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				saveClose(true);
			}
		}

		private void tbTranslation2_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				saveClose(true);
			}
		}

		private void tbTranslation3_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				saveClose(true);
			}
		}

		private void Phrase_FormClosing(object sender, FormClosingEventArgs e)
		{
			saveClose(false);

		}

		private void button1_Click(object sender, EventArgs e)
		{
			saveClose(true);
		}
	}
}
